import fever.utils.StringUtil;

/*-----------------------------------------------------------------------------
	The contents of this file are subject to the Mozilla Public License
	Version 1.1 (the "License"); you may not use this file except in
	compliance with the License. You may obtain a copy of the License at
	http://www.mozilla.org/MPL/
	
	Software distributed under the License is distributed on an "AS IS"
	basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
	License for the specific language governing rights and limitations
	under the License.
	
	The Original Code is Fever Framework code.
	
	The Initial Developer of the Original Code is Romain Ecarnot.
	Portions created by Initial Developer are Copyright (C) 2006
	the Initial Developer. All Rights Reserved.
	
	Contributor(s):
	
	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
-----------------------------------------------------------------------------*/

/**
 * {@code ShortcutString } class.
 * 
 * <p>Shortcut char is defined by {@link #SHORTCUT_CHAR} property.
 * 
 * <p>If an escape character is found just before specific character 
 * it will be removed from string.
 *  
 * <ul>
 *   <li>index : Number indicates shortcut position</li>
 *   <li>match : Boolean indicates if shortcut is enable</li>
 *   <li>text : New string value ( escape or shortcut char is removed )</li>
 *   <li>source : Original string</li>
 * </ul>
 * 
 * @author Romain Ecarnot
 */
class fever.structures.ShortcutString
{
	//-------------------------------------------------------------------------
	// Private properties
	//-------------------------------------------------------------------------
	
	private var _index : Number;
	private var _source : String;
	private var _enabled : Boolean;
	private var _text : String;
	
	
	//-------------------------------------------------------------------------
	// Public Properties
	//-------------------------------------------------------------------------
	
	/** Char delimiter for shortcut auto definition. */
	public static var SHORTCUT_CHAR : String = "&";
		
	/** Read only. String without shortcut marker. */
	public function get text() : String { return _text; }
	
	/** Read only. {@code true} if string has a shortcut marker. */
	public function get enabled() : Boolean { return _enabled; }
	
	/** Read only. Original string source ( with shortcut marker ). */
	public function get source() : String { return _source; }
	
	/** Read only. Shortcut marker position in string. */
	public function get index() : Number { return _index; }	
	
	
	//-------------------------------------------------------------------------
	// Public API
	//-------------------------------------------------------------------------
	
	/**
	 * Constructor.
	 */
	public function ShortcutString( caption : String )
	{
		_source = caption;
		
		_checkShortcut( caption );
	}
	
	/**
	 * Returns target shortcut marker char.
	 */
	public function getMarker() : String
	{
		return _text.charAt( _index ).toLowerCase();
	}
	
	/**
	 * Returns target shortcut marker key code.
	 */
	public function getMarkerID() : Number
	{
		return _text.charCodeAt( _index );
	}
	
	/**
	 * Returns string representation.
	 */
	public function toString() : String
	{
		return 'Shortcut ' + _enabled + ' [ ' + _text + ' | ' + _index + ' ]'; 
	}
	
	
	//-------------------------------------------------------------------------
	// Private implementation
	//-------------------------------------------------------------------------
	
	private function _checkShortcut( caption : String ) : Void
	{
		_index = caption.indexOf( SHORTCUT_CHAR );
		var a : Array;
		
		if( _index > -1 ) 
		{
			if( caption.substr( _index - 1, 1 ) == "\\" )
			{
				_text = StringUtil.removeAt( caption, _index - 1 );
				_enabled = false;
			}
			else
			{
				_text = StringUtil.removeAt( caption, _index );
				_enabled = true;
			}
		}
		else
		{
			_text = caption;
			_enabled = false;
		}
	}	
}